/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import junit.framework.Assert;
import nl.cloudfarming.client.messaging.Message;
import org.junit.Before;
import org.junit.Ignore;
import org.openide.util.NbBundle;
import org.uispec4j.Trigger;
import org.uispec4j.UISpec4J;
import org.uispec4j.Window;
import org.uispec4j.interception.WindowInterceptor;

/**
 *
 * @author Gerben Feenstra
 */
public class MessageNotifierGuiIT {

    private Message testMessage;
    private final String afzenderStr = "Afzender1";
    private final String titelStr = "Titel1";
    private final String msgStr = "MessageText1";
    private String dateReceivedStr = "";
    
    
    static {
        UISpec4J.init();
    }
    
    @Before
    public void setUp(){
        testMessage = new Message.Builder().setOrigin(afzenderStr).setTitle(titelStr).setMessageText(msgStr).setDateReceived(new Date()).build(); 
        dateReceivedStr = new SimpleDateFormat(NbBundle.getMessage(MessageDisplayFrame.class, "messaging.message.datereceived.format")).format(testMessage.getDateReceived());
    }
         
    @Ignore
    // Hudson: No X11 DISPLAY variable was set, but this program performed an operation which requires it.
    public void testMessageDisplayFrame(){       
     
        Trigger trigger = new MessageDisplayFrameTrigger();
        Window panel =  WindowInterceptor.run(trigger);
        
        //Test JLabels
        Component[] jLabels = panel.getSwingComponents(JLabel.class);

        boolean afzender = false;
        boolean title = false;
        boolean dateReceived = false;
        for (Component component : jLabels) {
            JLabel jLabel = (JLabel) component;
            if (jLabel.getText().equals(afzenderStr)) {
                afzender = true;
            }

            if (jLabel.getText().equals(titelStr)) {
                title = true;
            }

            if (jLabel.getText().equals(dateReceivedStr)) {
                dateReceived = true;
            }
        }
        Assert.assertTrue(afzender);
        Assert.assertTrue(title);
        Assert.assertTrue(dateReceived);
        
        // Test JTextArea (Message-content)
        Component[] jTextAreas = panel.getSwingComponents(JTextArea.class);
        boolean messageText = false;
        for (Component component : jTextAreas) {
            JTextArea jTextArea = (JTextArea) component;
            if (jTextArea.getText().equals(msgStr)) {
                messageText = true;
            }
        }           
        Assert.assertTrue(messageText);
    }

        

 
private class MessageDisplayFrameTrigger implements Trigger {
 
        public MessageDisplayFrameTrigger() {
        }
 
        @Override
        public void run() throws Exception {
            MessageDisplayFrame messagePopUp = new MessageDisplayFrame(testMessage);       
        }
    }  
}
